/**
 * @auth Ning
 * 
 * @purpose LOAD SWF, IMAGE FORM EXTERNAL...
 * 
 * @date 2009-8-27
 * 
 * 
 * */	
package com.components.loader
{
	import core.error.ErrorEvents;
	import core.error.Errors;
	
	import debug.Log;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	public class MediaLoader extends EventDispatcher implements I_Loader
	{

		//START...
		
		//PROPERTY...
		private var loader:Loader;
		
		private var path:String;
		
		private var domain:ApplicationDomain;
		
		private var _content:DisplayObject;	

		//CONSTRUCTOR...
		public function MediaLoader(path:String,domain:ApplicationDomain = null)
		{
		
			this.path = path;
			
			this.domain = !domain? ApplicationDomain.currentDomain : domain;
			
			init();	
		}
		
		
		//METHODS
		
		
		//init...
		private function init():void
		{
			
			loader = new Loader;
			
			configLoaderEventListeners();
			
		}
		
		
		//config loader event listeners...
		private function configLoaderEventListeners():void
		{
		
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadCompleteHandler);
			
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadIoErrorHandler);
			
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loadProgressHandler);
			
			loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
			
		}
		
		//security error handler...
		private function securityErrorHandler(evt:SecurityErrorEvent):void
		{
			
			dispatchEvent( new ErrorEvents(ErrorEvents.SECURITY_ERROR,new Errors(url)));
			
		}
		
		//load complete handler...
		private function loadCompleteHandler(evt:Event):void
		{
			
			Log.log("MEDIA LOAD SUCCESS: ",path);	
			
			try
			{
				
				_content = loader.content;	
				
			}catch(err:SecurityError)
			{
			
				_content = loader;
				
			}
									
			dispatchEvent(evt);
			
		}
		
		//load ioerror handler...
		private function loadIoErrorHandler(evt:IOErrorEvent):void
		{
						
			dispatchEvent( new ErrorEvents(ErrorEvents.IO_ERROR,new Errors(url)));
			
		}

		//load progress handler...
		private function loadProgressHandler(evt:ProgressEvent):void
		{
			
			dispatchEvent(evt);
			
		}		
		
		
		//get content... implements I_Loader interface...
		public function get content():*
		{
		
			return _content;
		
		}
		
		
		//start load... implements I_Loader interface...
		public function load():void
		{
		
			var request:URLRequest = new URLRequest(path);
			
			var context:LoaderContext = new LoaderContext(true,domain);
			
			loader.load(request,context);
		
		}
		
		
		// implements I_Loader interface... stop loading...
		public function stop():void
		{
			
			try{
			
				loader.close();
							
			}catch(err:Error){};
			
		}
		
		// set url...
		public function set url(_url:String):void
		{
			
			path = _url;
			
		}
		
		//get url...
		public function get url():String
		{
			
			return path;
			
		}

		//END
	}
}